class StructuresController < ApplicationController
	def new
		@structure = Structure.new
	end
	
	def create
		@structure = Structure.new(event_params)
	
		if @structure.save
			redirect_to @structure
		else
			render 'new'
		end
	end

	def all
		if params[:typology] != nil
			@structures = Structure.where(typology: params[:typology])
		else
			@structures = Structure.all
		end
		
		@typologies = Structure.distinct.pluck(:typology)
		
	end
	
	def best
		@structures = Structure.where(best: true)
	end
	
	def show
		@structure = Structure.find(params[:id])
		
		@guided_tour = params[:guided_tour] == 'true'
		
		if @guided_tour && @structure.best != true
		######## more quietly:
		#	params[:guided_tour] = false
		#	@guided_tour = false
		 
			raise "Illigal URL: Structure not valid for guided tour"
		end
		
		if @guided_tour
			@best_structures = Structure.where(best: true)
			current_index	= @best_structures.index(@structure)
			previous_index 	= (current_index-1)%@best_structures.size()
			next_index		= (current_index+1)%@best_structures.size()
			
			@previous 	= @best_structures.at(previous_index)
			@next 		= @best_structures.at(next_index)
		end
	end
	
	def related_events
		@structure = Structure.find(params[:id])
		@related_events = @structure.events
		@guided_tour = params[:guided_tour] == 'true'
	end
	
	def booking_table
		@structure = Structure.find(params[:id])
		@guided_tour = params[:guided_tour] == 'true'
	end
	
	def destroy
		@structure = Structure.find(params[:id])
		@structure.destroy
		
		redirect_to structures_all_path
	end
	
	private
		def event_params
			params.require(:structure).permit(:name, :typology_virtual, :description, :capacity, :equipments, :img_src, :preview_img_src, :best)
		end

end
